<!-- 需求列表  -->
<template>
  <pageHeader title="工位列表" :breadCrumb="['工厂建模', '工位管理']" />
  <div class="search-box">
    <div>
      <a-space>
        <a-input
          v-model="searchForm.keyWord"
          :style="{ width: '220px' }"
          placeholder="工位编码"
          @change="Search"
        />
        <a-select
          v-model="searchForm.enable"
          placeholder="状态"
          :style="{ width: '220px' }"
          @change="Search"
        >
          <a-option :value="''"> 全部 </a-option>
          <a-option :value="1"> 启用 </a-option>
          <a-option :value="2"> 停用 </a-option>
        </a-select>
        <a-button type="outline" @click="resetting">
          <template #icon> <icon-refresh /> </template>重置
        </a-button>
      </a-space>
    </div>
    <a-space>
      <a-button type="primary" @click="toDetails('add')">
        <template #icon>
          <icon-plus />
        </template>
        新增工位
      </a-button>
      <HeaderSettingCopy
        v-model:headerValues="headerValues"
        :define-headers="StationHerders.herders"
        :type="StationHerders.type"
      />
    </a-space>
  </div>
  <tableTurn style="padding: 0 10px" :tableData="getDemandAll" ref="tableRef">
    <a-table-column :width="70" align="center">
      <template #title> 序号 </template>
      <template #cell="{ rowIndex }">
        {{ rowIndex + 1 }}
      </template>
    </a-table-column>
    <a-table-column
      v-for="header in headerValues"
      :key="header.name"
      :width="header.width || 200"
      :title="header.name"
      :fixed="header.fixed"
      :data-index="header.code"
      align="center"
    >
      <template #cell="{ record }" v-if="!header.code">
        <template v-if="header.name === '状态'">
          <a-typography-text type="success" v-if="record.enable === 1">
            启用
          </a-typography-text>
          <a-typography-text type="danger" v-if="record.enable === 2">
            停用
          </a-typography-text>
        </template>
      </template>
    </a-table-column>
    <a-table-column :width="120" fixed="right" title="操作" align="center">
      <template #cell="{ record }">
        <!-- <a-space>
          <a-link @click="toDetails('edit', record)"> 修改 </a-link>
          <a-popconfirm
            content="是否删除该数据?"
            type="error"
            @ok="del(record.id)"
          >
            <a-link status="danger"> 删除 </a-link>
          </a-popconfirm>
        </a-space> -->
        <a-tooltip content="修改">
          <IconFont
            type="icon-edit"
            :size="26"
            :style="{
              marginRight: '10px',
              cursor: 'pointer',
            }"
            @click="toDetails('edit', record)"
          />
        </a-tooltip>
        <a-tooltip content="删除">
          <a-popconfirm
            content="是否删除该数据?"
            type="error"
            @ok="del(record.id)"
          >
            <IconFont
              type="icon-delete"
              :size="26"
              :style="{
                marginRight: '10px',
                cursor: 'pointer',
              }"
            />
          </a-popconfirm>
        </a-tooltip>
      </template>
    </a-table-column>
  </tableTurn>
  <StationModal ref="refModal" @refresh="Search" />
</template>

<script lang="ts" setup>
  import { reactive, onMounted, ref, nextTick } from 'vue';
  import { useRouter } from 'vue-router';
  import { GetStationPage, DelStation } from '@/api/FactoryModeling';

  import { StationHerders } from '@/utils/tableHerders';
  import { Message } from '@arco-design/web-vue';
  import StationModal from './StationModal.vue';

  const router = useRouter();
  let searchForm = reactive<any>({});
  const tableRef = ref();

  let refModal = ref();
  const toDetails = (mode: string, data?: any) => {
    refModal.value.loadData(data);
  };

  const getDemandAll = async (page: any) => {
    try {
      let res = await GetStationPage({
        ...page,
        ...searchForm,
      });
      return Promise.resolve(res);
    } catch (err: unknown) {
      return Promise.reject();
    }
  };

  const Search = () => {
    tableRef.value.loadData();
  };
  const resetting = () => {
    for (const key in searchForm) {
      delete searchForm[key];
    }
    tableRef.value.loadData();
  };
  const del = async (id: number) => {
    await DelStation({ id });
    tableRef.value.loadData();
  };

  let headerValues = ref<any[]>([]);
</script>

<style scoped lang="less">
  .search-box {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    padding: 10px;
    margin-top: -7px;
  }
</style>
